package com.myfum.wingplayer.entity
{
	import com.myfum.wingplayer.entity.*;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.utils.describeType;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;
	
	import mx.collections.ArrayCollection;
	import mx.formatters.DateFormatter;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.soap.WebService;

	public class Data extends EventDispatcher
	{
		public function Data()
		{
			classReference = getDefinitionByName(getQualifiedClassName(this)) as Class;
		}
		
		protected var wsdl:String;
		
		protected var classReference:Class;
		
		protected var _retrieveURL:String;
		protected var _commitURL:String;
		
		public function copyValuesFromObject(obj:Object):void{
			for(var i:String in obj){
				if(this.hasOwnProperty(i)){
					this[i] = obj[i];
				}
			}
		}
		
		protected function _loadWSDL():void{
			var service:WebService = new WebService();
			service.wsdl = "assets/testdata/schedule.xml";
			service.addEventListener(ResultEvent.RESULT, onResult);
			service.addEventListener(FaultEvent.FAULT, onFault);
			service.loadWSDL();
		}
		
		protected function _retrieve(request:URLRequest):void{
			/*request.url = _retrieveURL;*/
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onComplete);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
			loader.load(request);
		}
		
		private function onComplete(evt:Event):void{
			analyseXML(XML(evt.target.data));
		}
		
		private function onIOError(evt:IOErrorEvent):void{
			trace(evt);
		}
		
		private function onResult(evt:ResultEvent):void{
			trace(evt.result.toString());
		}
		
		private function onFault(evt:FaultEvent):void{
			trace(evt.fault.message);
		}
		
		protected function analyseXML(xml:XML):void{
			var classReference:Class = getDefinitionByName(getQualifiedClassName(this)) as Class;
			for each(var i:XML in xml.children()){
				var referName:String = i.name().toString();
				switch(referName){
					case "int":
					case "Number":
						classReference(this)[i.@id] = Number(i);
						break;
					case "uint":
						classReference(this)[i.@id] = getColor(i);
						break;
					case "Boolean":
						classReference(this)[i.@id] = Boolean(i);
						break;
					case "String":
						classReference(this)[i.@id] = String(i);
						break;
					case "Date":
						classReference(this)[i.@id] = DateFormatter.parseDateString(i);
						break;
					case "ArrayCollection":
						classReference(this)[i.@id] = readArrayCollection(i);
						break;
					default:
						var Define:Class = getDefinitionByName(referName) as Class;
						classReference(this)[i.@id] = new Define();
						classReference(this)[i.@id].analyseXML(i);
						break;
				}
			}
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
		private function readArrayCollection(xml:XML):ArrayCollection{
			var arr:ArrayCollection = new ArrayCollection();
			for each(var i:XML in xml.children()){
				var Refer:Class = getDefinitionByName("com.myfum.wingplayer.entity." + i.name().toString()) as Class;
				var refer:* = new Refer();
				refer.analyseXML(i);
				arr.addItem(refer);
			}
			return arr;
		}
		
		private function getColor(value:String):uint{
			if(value.charAt(0) == "#"){
				value = value.replace("#", "0x");
			}
			return uint(value);
		}
	}
}















